<?php if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' ); ?>

<?php 
/**
 * This file displays the part payment option shown in the products.
 */
include_once(CLASSPATH . '/payment/ps_kreditor_konto.cfg.php');
if (KREDITOR_KONTO_DISPLAY_KLARNA_CART == 'Y') {

	$dbCountrie = new ps_DB;
	$q  = "SELECT * from #__{vm}_user_info WHERE user_id='" . $_SESSION['auth']['user_id'] . "' AND address_type='BT'";
	$dbCountrie->query($q);
	$country = $dbCountrie->f('country');
	
	$order_total*100;
	
	$temp = $CURRENCY_DISPLAY->getFullValue($order_total);
	$current_currency;
	if($_SESSION['product_currency'] == $_SESSION['vendor_currency']) {
		$temp = $_SESSION[product_currency];
	}
	if(strstr($temp, "SEK")) {
		$current_currency = 'KRED_SEK';
	} else if(strstr($temp, "NOK")) {
		$current_currency = 'KRED_NOK';
	} else if(strstr($temp, "DKK")) {
		$current_currency = 'KRED_DKK';
	} else if(strstr($temp, "EUR")) {
		$current_currency = 'KRED_EUR';
	} else {
		if(isset($_SESSION['product_currency'])) {
			$current_currency = 'KRED_' . $_SESSION['product_currency'];
		} else {
			$current_currency = 'KRED_' . $_SESSION['vendor_currency'];
		}
	}
	
	$sum = 1000;
	switch ($current_currency) {
		case 'KRED_SEK':
		case 'KRED_NOK':
		case 'KRED_DKK':
			$sum = 1000;
			break;
		default:
			$sum = 100;
		break;
	}
	
	if($order_total >= $sum) {
		//If not logged on: continue.
		//If logged on and from valid countrie: continue.
		//Else: move along elsewhere. Nothing will be printed.
		if(!isset($country) || in_array($country, getValidCountrieCodes())) {
			$joomlaLang = getJoomlaOrElseVendorLanguage();
			//If not logged on: then check if joomla language(if not joomla valid lang, then vendor lang)
			//and if that is valid then continue.
			//ElseIf: logged on then above statement takes care of all.
			//Else: move along elsewhere. Nothing will be printed.
			if(!isset($country) && in_array($joomlaLang, getValidCountrieCodes())) {
				$country = $joomlaLang;
			}
			
			/**
			 * If a countrie is set(and hence valid), then continue, else just move along...
			 */
			if(isset($country)) {
				include_once(CLASSPATH . '/payment/ps_kreditor.cfg.php');
				include_once(CLASSPATH . '/payment/ps_kreditor_del.php');
				//include_once(CLASSPATH . '/payment/ps_kreditor_del.php');
				
//				$temp = $CURRENCY_DISPLAY->getFullValue($order_total);
//				$current_currency;			
//				if($_SESSION['product_currency'] == $_SESSION['vendor_currency']) {
//					$temp = $_SESSION[product_currency];
//				}
//				if(strstr($temp, "SEK")) {
//					$current_currency = 'KRED_SEK';
//				} else if(strstr($temp, "NOK")) {
//					$current_currency = 'KRED_NOK';
//				} else if(strstr($temp, "DKK")) {
//					$current_currency = 'KRED_DKK';
//				} else if(strstr($temp, "EUR")) {
//					$current_currency = 'KRED_EUR';
//				} else {
//					if(isset($_SESSION['product_currency'])) {
//						$current_currency = 'KRED_' . $_SESSION['product_currency'];
//					} else {
//						$current_currency = 'KRED_' . $_SESSION['vendor_currency'];
//					}
//				}
				
				/**
				 * If error searching the currency use this to check that the given currency is the same as
				 * the below commented echo.
				 */
				//echo '<br />' . $temp . '<br />' . $current_currency . '<br />';
						
				$countryNumber = 0;
				switch($country) {
					case "SWE": $countryNumber = 209; break;
					case "NOR": $countryNumber = 164; break;
					case "DNK": $countryNumber = 59; break;
					case "DEU": $countryNumber = 81; break;
					case "FIN": $countryNumber = 73; break;
					case "NLD": $countryNumber = 154; break;
				}
				
				activateLanguage();
				$langInfo = new Singleton("language");
				
				$lowest = 1000000;
				//echo '<br />$order_total: ' . $order_total . <br />';
			
				/**
				 * This first check is a really ugly peice of code to check if germany account pclass
				 * should be used or not.
				 * This should be remade in v2.36
				 */
	//			if($current_currency == 'KRED_EUR' && strtolower($langInfo->Country) == "deu") {
	//				$db = new ps_DB;
	//				$q = "SELECT * FROM #__{vm}_kreditor_pclass WHERE interest = '1495' AND description = '1/24 avbetalningskonto' AND currency = 'KRED_EUR' ORDER BY months DESC";
	//				$db->query($q);
	//				if($db->next_record()) {
	//					$monthly_payment = calcMonthlyCost($order_total, $db->f("pclass_id"), $db->f("interest"), $db->f("months"), $db->f("handlingfee"), $current_currency, 0);
	//			
	//					if($monthly_payment == -2) {
	//						continue;
	//					}
	//					//echo '='. $monthly_payment/100 .' ';
	//					$lowest = min($lowest, round($monthly_payment/100, 0));
	//				}
	//			} else {
					$db = new ps_DB;
					//REMOVE "AND description = '1/24 avbetalningskonto'" from the query to make it use all available pclasses instead.
					//It is removed, again.. READD:  AND "description = '1/24 avbetalningskonto'" if wanted...
					$q = "SELECT * FROM #__{vm}_kreditor_pclass WHERE country = $countryNumber ORDER BY months DESC;";
					$db->query($q);
				
					while($db->next_record()) {
						/**
						 * Germany.. continue..
						 */
	//					if($db->f("interest") == '1495' && $db->f("description") == '1/24 avbetalningskonto' && $db->f("currency") == 'KRED_EUR') {
	//						continue;
	//					}
						getMontlyCost($order_total*100, $db->f("pclass_id"), 1, $result);
						//echo 'TL: ' . $result . '<br>';
						//$monthly_payment = calcMonthlyCost($order_total, $db->f("pclass_id"), $db->f("interest"), $db->f("months"), $db->f("handlingfee"), $current_currency, 0);
						
						if($result == -2) {
							continue;
						}
						//echo '='. $monthly_payment/100 .' ';
						$lowest = min($lowest, round($result/100, 0));
					}
				//}
				if($lowest != 1000000) {
					$currency_display = ps_vendor::get_currency_display_style( $vendor_currency_display_style );
					if( $GLOBALS['product_currency'] != $vendor_currency ) {
						$currency_display["symbol"] = $GLOBALS['product_currency'];
					}
					$GLOBALS['CURRENCY_DISPLAY3'] = new CurrencyDisplay($currency_display["id"], $currency_display["symbol"], 0, $currency_display["sdecimal"], $currency_display["thousands"], $currency_display["positive"], $currency_display["negative"]);
					?>
					<tr>
						<td colspan="4" align="right"><?php echo $langInfo->TEXT_MONTHLY_COST ?>: </td>
						<td colspan="3" align="right">
							<span style="color: red;" class="klarna_pp_color"><b><?php echo $GLOBALS['CURRENCY_DISPLAY3']->getFullValue($lowest) ?></b></span>
						</td>
					</tr>
			<?php }
			}
		}
	}
}?>